# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=kicad
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-meta"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-footprints"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-symbols"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-templates"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D")
pkgver=5.1.5
pkgrel=1
pkgdesc="Software for the creation of electronic schematic diagrams and printed circuit board artwork (mingw-w64)"
arch=("any")
url="http://www.kicad-pcb.org"
license=("GPLv3+")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-glm"
         "${MINGW_PACKAGE_PREFIX}-ngspice"
         "${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-wxPython"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-oce")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/KiCad/kicad-source-mirror/archive/${pkgver}.tar.gz"
        "${_realname}-i18n-${pkgver}.tar.gz"::"https://github.com/KiCad/kicad-i18n/archive/${pkgver}.tar.gz"
        "${_realname}-footprints-${pkgver}.tar.gz"::"https://github.com/KiCad/kicad-footprints/archive/${pkgver}.tar.gz"
        "${_realname}-symbols-${pkgver}.tar.gz"::"https://github.com/KiCad/kicad-symbols/archive/${pkgver}.tar.gz"
        "${_realname}-templates-${pkgver}.tar.gz"::"https://github.com/KiCad/kicad-templates/archive/${pkgver}.tar.gz"
        "${_realname}-packages3D-${pkgver}.tar.gz"::"https://github.com/KiCad/kicad-packages3D/archive/${pkgver}.tar.gz")
sha256sums=('1e1a643ab7a8b0bb677c3059263f9bd95c91998176cd36e72b3bc9f4821922cd'
            'aec6902def684ffee62bb7d84edc167151d555ab743cc81b10053207b4e842a3'
            'c2bb5a126e49020134b75dc27d2ee4a9cd56cf300bb16c397ac580fd43339f76'
            '47abb25a88e8f2d9a623d21476bb7ee5a60f5e68e55dcf388842610f0bad7e06'
            'f08f857eee9b88cc89d1579c66046e01d752cf696d468d8ac2465066d379e406'
            'b1c35e686865faf8a9b08d3843b188e90b4088f0b1e5f3f0393fac833a22a749')

_doc=("ca" "de" "en" "es" "fr" "id" "it" "ja" "nl" "pl" "ru" "zh")
 _sub=("footprints" "symbols" "templates" "packages3D")

prepare() {
  cd ${_realname}-source-mirror-${pkgver}
}

build() {
  cd "${srcdir}"
  msg2 "Build kicad"
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;DCMAKE_PREFIX_PATH=;DEFAULT_INSTALL_PATH=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_PREFIX_PATH=${MINGW_PREFIX} \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DDEFAULT_INSTALL_PATH=${MINGW_PREFIX} \
    -DOPENSSL_ROOT_DIR=${MINGW_PREFIX} \
    -DKICAD_SCRIPTING=ON \
    -DKICAD_SCRIPTING_MODULES=ON \
    -DKICAD_SCRIPTING_WXPYTHON=ON \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python2.exe \
    ../${_realname}-source-mirror-${pkgver}

  make

  cd "${srcdir}"
  msg2 "Build translations"
  [[ -d build-i18n ]] && rm -rf build-i18n
  mkdir -p build-i18n && cd build-i18n

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-i18n-${pkgver}

  for _s in ${_sub[@]}; do
    cd ${srcdir}
    msg2 "Build ${_s}"
    [[ -d build-${_s} ]] && rm -rf build-${_s}
    mkdir -p build-${_s} && cd build-${_s}
  
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G "MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}-${_s}-${pkgver}
  done
}

for _doclang in ${_doc[@]}; do _doc_pkg+=("\"${MINGW_PACKAGE_PREFIX}-${_realname}-doc-${_doclang}: Documentation for KiCad (mingw-w64)\""); done
for _feature in ${_sub[@]}; do _sub_pkg+=("\"${MINGW_PACKAGE_PREFIX}-${_realname}-${_feature}\""); done

eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}() {

  msg2 \"Package kicad\"
  optdepends+=(${_doc_pkg[@]} ${_sub_pkg[@]})
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-git)
  
  cd \${srcdir}/build-${MINGW_CHOST}
  make DESTDIR=\${pkgdir} install

  cd \${srcdir}/build-i18n
  make DESTDIR=\${pkgdir} install
}"

eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}-meta() {
  depends=(${MINGW_PACKAGE_PREFIX}-${_realname}
           ${_sub_pkg[@]})
}"

for _part in ${_sub[@]}; do
  eval "package_${MINGW_PACKAGE_PREFIX}-${_realname}-${_part}() {
    options=('!strip')
    conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-${_part}-git)
    replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-${_part}-git)
    msg2 \"Package ${_part}\"
    cd \${srcdir}/build-${_part}
    make DESTDIR=\${pkgdir} install
  }"
done
